<?php
namespace app\shop\controller\sys_admin;
use app\AdminController;
use app\shop\model\StoreModel;
use think\Db;

/**
 * 商品品牌
 * Class Index
 * @package app\store\controller
 */
class Store extends AdminController
{
    public $is_supplyer = false;//是否后台供应商管理
    public $supplyer_id = 0;//当前操作的供应商ID
	//*------------------------------------------------------ */
	//-- 初始化
	/*------------------------------------------------------ */
   public function initialize()
   {
   		parent::initialize();
		$this->Model = new StoreModel();
        $this->assign('is_supplyer', $this->is_supplyer);
    }
	//*------------------------------------------------------ */
	//-- 首页
	/*------------------------------------------------------ */
    public function index()
    {
        $this->getList(true);
        return $this->fetch();
    }

    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false)
    {
        $where = [];
        $where[] = ['is_del','=',0];
        if ($this->supplyer_id > 0) {
            $where[] = ['supplyer_id', '=', $this->supplyer_id];
        } elseif ($this->is_supplyer == true) {
            $where[] = ['supplyer_id', '>', 0];
        } else {
            $where[] = ['supplyer_id', '=', 0];
        }
        $search['keyword'] = input('keyword', '', 'trim');
        if (empty($search['keyword']) == false) $where[] = ['store_name', 'like', '%' . $search['keyword'] . '%'];
        $data = $this->getPageList($this->Model, $where);
        foreach ($data['list'] as &$value){
//            $value['type_text'] = $this->Model->type[$value['type']];
            $value['order_count'] = Db::name('shop_order_info')->where('sid',$value['id'])->where('shipping_status',2)->count();
        }
        $this->assign("data", $data);
        $this->assign("search", $search);
        if ($runData == false) {
            $data['content'] = $this->fetch('list')->getContent();
            unset($data['list']);
            return $this->success('', '', $data);
        }
        return true;
    }
    /*------------------------------------------------------ */
    //-- 信息页调用
    //-- $data array 自动读取对应的数据
    /*------------------------------------------------------ */
    public function asInfo($data){
        $data['lnglat'] = $data['longitude'].','.$data['latitude'];
        return $data;
    }

	/*------------------------------------------------------ */
	//-- 添加前处理
	/*------------------------------------------------------ */
    public function beforeAdd($data) {
		if (empty($data['store_name']) ) return $this->error('店铺名称不能为空.');
		$map['store_name'] = $data['store_name'];
		$count = $this->Model->where($map)->count();
		if ($count > 0) return $this->error('已存在相同的店铺名称，不允许重复添加.');
        $lnglat = explode(',',$data['lnglat']);
        $data['longitude'] = $lnglat[0];
        $data['latitude'] = $lnglat[1];
        unset($data['lnglat']);
		$data['add_time'] = time();
        $data['province_name'] = Db::name('t_areas')->where(['id'=>$data['province'],'type' => 1])->value('name');
        $data['city_name'] = Db::name('t_areas')->where(['id'=>$data['city'],'type' => 2])->value('name');
        $data['district_name'] = Db::name('t_areas')->where(['id'=>$data['district'],'type' => 3])->value('name');
        $data['street_name'] = Db::name('t_areas')->where(['id'=>$data['street'],'type' => 4])->value('name');
        $data['supplyer_id'] = $this->supplyer_id;
		return $data;
	}

	/*------------------------------------------------------ */
	//-- 修改前处理
	/*------------------------------------------------------ */
    public function beforeEdit($data){

		if (empty($data['store_name']) ) return $this->error('店铺名称不能为空.');
		$map['id'] = $data['id'];
		//验证数据是否出现变化
		$where[] = ['id','<>',$map['id']];
		$where[] = ['store_name','=',$data['store_name']];
		$count = $this->Model->where($where)->count();
		if ($count > 0) return $this->error('已存在相同的店铺名称，不允许重复添加！');
        $lnglat = explode(',',$data['lnglat']);
        $data['longitude'] = $lnglat[0];
        $data['latitude'] = $lnglat[1];
        unset($data['lnglat']);
        $data['province_name'] = Db::name('t_areas')->where(['id'=>$data['province'],'type' => 1])->value('name');
        $data['city_name'] = Db::name('t_areas')->where(['id'=>$data['city'],'type' => 2])->value('name');
        $data['district_name'] = Db::name('t_areas')->where(['id'=>$data['district'],'type' => 3])->value('name');
        $data['street_name'] = Db::name('t_areas')->where(['id'=>$data['street'],'type' => 4])->value('name');
		return $data;
	}
	/*------------------------------------------------------ */
	//-- 删除分类
	/*------------------------------------------------------ */
	public function del(){
		$map['id'] = input('id',0,'intval');
		if ($map['id']<1) return $this->error('传递参数失败！');
		$this->Model->where($map)->update(['is_del' => 1]);
		return $this->success('操作成功.');
	}

    public function ajaxEdit(){
        $id = input('id',0,'intval');
        $field = input('field','','trim');
        if ($id=='' || $field=='') return $this->error('缺少必要传参.');
        $row = $this->Model->find($id);
        if ($field == 'status'){
            $upArr['status'] = $row['status'] == 1 ? 0: 1;
        }
        $this->Model->save($upArr,['id'=>$id]);
        return $this->success();
    }
}
